package com.xfb.code.service;

import com.xfb.code.dao.CodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Random;

/**
 * @Auther: Xing Fang Bin
 * @Date: 2021/10/29/16:28
 * @Description:
 */
@Service
public class CodeService {

    @Autowired
    private CodeMapper codeMapper;

    public boolean createCode(String email) {
        Date now = new Date();
        long afterTime = now.getTime() + 180000;
        Date afterDate = new Date();
        afterDate.setTime(afterTime);
        return codeMapper.insertCode(email, getRandom(),now ,afterDate)>0;
    }

    private String getRandom(){
        Random random = new Random();
        int randomInt = random.nextInt(1000000) + 1;
        while (String.valueOf(randomInt).length() != 6) {
            randomInt = random.nextInt(1000000) + 1;
        }
        return String.valueOf(randomInt);  //生成随机6位数
    }

}
